#import "MailViewController.h"
#import "IDominoAppDelegate.h"


@implementation MailViewController
@synthesize _mailElementArray;

- (id)initWithStyle:(UITableViewStyle)style {
 if (self = [super initWithStyle:style]) {
	 NSString *string =[NSString stringWithFormat:@"%@, %@, %@, %@", NSLocalizedString(@"Inbox", @"Inbox title"), 
						NSLocalizedString(@"Drafts", @"Drafts title"), NSLocalizedString(@"Sent", @"Sent title"), 
						NSLocalizedString(@"All", @"All title")];
	 self._mailElementArray = [string componentsSeparatedByString: @", "];
	 self.tabBarItem.image=[UIImage imageNamed:@"TabMailView.png"];
	 self.title = NSLocalizedString(@"Mailboxes", @"Mailboxes title");
	 IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
	 UIBarButtonItem *composeItemMail = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemCompose
																					  target:appDelegate action:@selector(composeAction:)];
	 self.navigationItem.rightBarButtonItem=composeItemMail;
	 
	 UIBarButtonItem *localItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Online", @"") style:UIBarButtonItemStylePlain target:appDelegate action:@selector(localAction:)];
	 self.navigationItem.leftBarButtonItem=localItem;
	 [composeItemMail release];
	 [localItem release];
	 
	 self.tableView.rowHeight = 65.0;
	 self.tableView.delegate = self;
	 self.tableView.dataSource = self;
	 
 }
 return self;

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [_mailElementArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MailBoxViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.textLabel.font=[UIFont boldSystemFontOfSize:24];
		cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
	NSString* mailElement=[_mailElementArray objectAtIndex:indexPath.row];
	cell.textLabel.text=mailElement;
    // Configure the cell

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
	NSUInteger row = indexPath.row;
	
    if (row != NSNotFound) {	
		IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
		appDelegate.mailListViewController.title = [_mailElementArray objectAtIndex:indexPath.row];
		if(indexPath.row==0)
			appDelegate.currentMailBox=NSLocalizedString(@"Inbox", @"Inbox title");
		else if(indexPath.row==1)
			appDelegate.currentMailBox=NSLocalizedString(@"Drafts", @"Drafts title");
		else if(indexPath.row==2)
			appDelegate.currentMailBox=NSLocalizedString(@"Sent", @"Sent title");
		else if(indexPath.row==3)
			appDelegate.currentMailBox=NSLocalizedString(@"All", @"All title");
		[appDelegate.mailListViewController setDisplayList:nil];
		[[self navigationController] pushViewController:appDelegate.mailListViewController animated:YES];
		[NSThread detachNewThreadSelector:@selector(getMailList) toTarget:appDelegate withObject:nil];
	}
}




- (void)dealloc {
	[_mailElementArray release];
    [super dealloc];
}

@end

